﻿using System;
using System.Windows.Forms;

namespace D3Trader
{
    public partial class Form1 : Form
    {
        public enum Popularity
        {
            Low = 1,
            High = 2
        }

        public enum AuctionType
        {
            Bid = 1,
            Buyout = 2
        }

        public Form1()
        {
            InitializeComponent();
            lowRadioButton.Checked = true;

            // Load settings from App Config file
            Trader.LoadConfig();
        }

        private void calculateButton_Click(object sender, EventArgs e)
        {
            if(traderValueTxtBox.Text == String.Empty)
                return;

            double value = Convert.ToDouble(traderValueTxtBox.Text);

            if (socketsUpDown.Value > 0)
            {
                int openSockets = Convert.ToInt32(socketsUpDown.Value);
                if (lowRadioButton.Checked)
                {
                    bidLabel.Text = CalculatePrice(value, Popularity.Low, AuctionType.Bid, openSockets).ToString();
                    buyoutLabel.Text = CalculatePrice(value, Popularity.Low, AuctionType.Buyout, openSockets).ToString();
                }
                else if (highRadioButton.Checked)
                {
                    bidLabel.Text = CalculatePrice(value, Popularity.High, AuctionType.Bid, openSockets).ToString();
                    buyoutLabel.Text = CalculatePrice(value, Popularity.High, AuctionType.Buyout, openSockets).ToString();
                }
                
            }
            else if (lowRadioButton.Checked)
            {
                bidLabel.Text = CalculatePrice(value, Popularity.Low, AuctionType.Bid).ToString();
                buyoutLabel.Text = CalculatePrice(value, Popularity.Low, AuctionType.Buyout).ToString();
            }
            else if (highRadioButton.Checked)
            {
                bidLabel.Text = CalculatePrice(value, Popularity.High, AuctionType.Bid).ToString();
                buyoutLabel.Text = CalculatePrice(value, Popularity.High, AuctionType.Buyout).ToString();
            }
        }

        private int CalculatePrice(double vendorPrice, Popularity p, AuctionType type)
        {
            double value = 0;

            switch (type)
            {
                case AuctionType.Bid:
                    switch (p)
                    {
                        case Popularity.Low:
                            value = vendorPrice * Convert.ToDouble(Popularity.Low) * Trader.LowBid;
                            break;
                        case Popularity.High:
                            value = vendorPrice * Convert.ToDouble(Popularity.High) * Trader.HighBid;
                            break;
                    }
                    break;
                case AuctionType.Buyout:
                    switch (p)
                    {
                        case Popularity.Low:
                            value = vendorPrice * Convert.ToDouble(Popularity.Low) * Trader.LowBuy;
                            break;
                        case Popularity.High:
                            value = vendorPrice * Convert.ToDouble(Popularity.High) * Trader.HighBuy;
                            break;
                    }
                    break;
            }

            return Convert.ToInt32(value);
        }

        private int CalculatePrice(double vendorPrice, Popularity p, AuctionType type, int sockets)
        {
            double value = CalculatePrice(vendorPrice, p, type);

            switch (sockets)
            {
                case 0: // Do Nothing
                    break;
                case 1:
                    value = value * Trader.Socket1Modifier;
                    break;
                case 2:
                    value = value * Trader.Socket2Modifier;
                    break;
                case 3:
                    value = value * Trader.Socket3Modifier;
                    break;
                default:    // Do Nothing
                    break;
            }
            return Convert.ToInt32(value);
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            bidLabel.Text = "0";
            buyoutLabel.Text = bidLabel.Text;
            socketsUpDown.Value = 0;
            traderValueTxtBox.Text = String.Empty;
            lowRadioButton.Checked = true;
            highRadioButton.Checked = false;
        }

        private void traderValueTxtBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                && !char.IsDigit(e.KeyChar)
                && e.KeyChar != '.')
            {
                e.Handled = true;
            }

            // only allow one decimal point
            if (e.KeyChar == '.'
                && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
        }

        private void showCredits()
        {
            Form credits = new Credits();
            credits.Show();
        }

        private void copyBidButtom_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(bidLabel.Text);
        }

        private void copyBuyoutButton_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(buyoutLabel.Text);
        }

        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            showCredits();
        }

        private void createdByLabel_Click(object sender, EventArgs e)
        {
            showCredits();
        }

        private void viciousLabel_Click(object sender, EventArgs e)
        {
            showCredits();
        }

        private void versionLabel_Click(object sender, EventArgs e)
        {
            showCredits();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            showCredits();
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form settings = new Settings();
            settings.Show();
        }
    }
}
